<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function login(){
        $Newsmodel=M('content');
        $tab['version']=array('like','%'.trim($_POST['version']).'%');
        $tab['category']=array('like','%'.trim($_POST['category']).'%');
        $tab['title']=array('like','%'.trim($_POST['title']).'%');
        $count=$Newsmodel->where($tab)->count();       
        $Page = new \Think\Page1($count,4);
        $show = $Page->show();  
        $data = $Newsmodel->where($tab)->limit($Page->firstRow.','.$Page->listRows)->select();                        
        $this->assign('show',$show);        
        $this->assign('data',$data);   
        $this->assign('count',$count);         
        $this->display();        
    }
    public function dologin(){       
       $modelUser=D('User');      
       $data=$modelUser->dologins(I('post.name'),I('post.pass'));   
        if(empty($data)){   
            $output=array();
            $output['info']="账号或密码错误";
            $output['url']=U('login');
            $output['status']=1;
            print json_encode($output);
            exit;
          // $this->error('账号或密码错误','login');
        }else{                            
            session('id',$data['id']);
             session('name',$data['name']);
            session('isLogin',true);
            $output=array();
            $output['info']="登录成功";
            $output['url']=U('login');
            $output['status']=0;
            print json_encode($output);
            exit;
           // $this->success('登录成功','login');
        }
    }
    public function logout(){
        session(null);
        $this->success('退出成功','login');
    }

//    public function Usercenter() {
//        if(session('isLogin')!=true){
//            $this->register('login');
//            exit;
//        }
//        $Newsmodel=M('content');
//        $data=$Newsmodel->select();
//        $this->assign('data',$data);
//        $this->display();
//    }
    public function register(){
        $this->display();
    }
    public function doregister(){   
        $modelUser=D('user');
        $data['mail']=$_POST['mail'];
        $map=$modelUser->doregisters($_POST['mail'],$_POST['name']);
        require_once LIB_PATH. 'custom/securimage/securimage.php';
        $securimage = new\ Securimage();
        $captcha=I('post.captcha');
        if(empty($map)){
       if ($securimage->check($captcha) == false){
        $this->error('验证码错误');        
       }else if($data['mail']==I('post.conf_mail')){
            $modelUser=D('user');
            $modelUser->register($_POST['name'],$_POST['pass'],$_POST['mail']);
            $this->success('注册成功','login');
        }else{
            $this->error('两次的邮箱不同','register');
        }
    }else{
        $this->error('用户名重复','register');
    }
    }
    public function captcha() {
        require_once LIB_PATH. 'custom/securimage/securimage.php';
        $img = new \Securimage();
        if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);
        $img->show();             
    }
    public function password(){
        $this->display();
    }  
    public function read(){
        $Newsmodel=M('content');
        $data=$Newsmodel->where('id='.$_GET['id'])->find();
        $this->assign('data',$data);
        $this->display();       
    }
    public function update(){
        $Newsmodel=M('content');
        $map['id']=$_GET['id'];
        $data=$Newsmodel->where($map)->find();
        $this->assign('data',$data);
        $this->display();
                
    }
}